﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using WeChat.Common;
using WeChat.Common2;

namespace WeChat.JsSDK
{
    public class GetSdkParams
    {

       /*========================config验证参数===========================*/
                   /*ticket、appid、时间戳、随机数、签名*/
        //ticket
        public static string getTicket()
        {
            return GetTicket.getTicket();
        }
        //appid
        public static string getAppId()
        {
            //return ConfigurationSettings.AppSettings["appId"].ToString();
            return Config.getAppID();
        }
        //时间戳
        public static string getTimestamp()
        {
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return Convert.ToInt64(ts.TotalSeconds).ToString();
        }
        //随机数生成,参数为长度
        public static string getNonceStr(int len)
        {
            string s = "123456789abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ"; 
            Random r = new Random();
            char[] m = new char[len];
            for (int i = 0; i < len; i++)
            {
                m[i] = s[r.Next(s.Length)];
            }
            return new string(m);
        }
        //JsSDK签名生成
        public static string getSignature(string url)
        {
            //timestamp = getTimestamp();
            //noncestr = getNonceStr(16);
            string jsapi_ticket = getTicket();
            //ApplicationLog.Write("jsapi_ticket：" + jsapi_ticket);
            string signature = string.Format("jsapi_ticket={0}&noncestr={1}&timestamp={2}&url={3}", jsapi_ticket, nonceString, timestamp, url);
            //ApplicationLog.Write("加密前string1：" + signature);
#pragma warning disable CS0618 // “FormsAuthentication.HashPasswordForStoringInConfigFile(string, string)”已过时:“The recommended alternative is to use the Membership APIs, such as Membership.CreateUser. For more information, see http://go.microsoft.com/fwlink/?LinkId=252463.”
            signature = FormsAuthentication.HashPasswordForStoringInConfigFile(signature, "sha1");
#pragma warning restore CS0618 // “FormsAuthentication.HashPasswordForStoringInConfigFile(string, string)”已过时:“The recommended alternative is to use the Membership APIs, such as Membership.CreateUser. For more information, see http://go.microsoft.com/fwlink/?LinkId=252463.”
            //ApplicationLog.Write("加密后string2：" + signature);
            return signature.ToLower();
        }
        //生成JsSDKconfig配置信息串
        //public static string getWxConfigStr(string url)
        //{
        //    //string timestamp = getTimestamp();
        //    //string nonceString = getNonceStr(16);
        //    string wxconfig = "appId:'" + getAppId() + "',\n timestamp:'" + timestamp + "',\n nonceStr:'" + nonceString + "',\n signature:'" + getSignature(timestamp,nonceString,url) + "'";
        //    ApplicationLog.Write("JsSDK配置信息串：" + wxconfig);
        //    return wxconfig;
        //}

        public static string timestamp = getTimestamp();
        public static string nonceString = getNonceStr(16);
    }
}
